<?php
/**
 * @title 2719. 统计整数数目 - 数位DP
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param String $num1
     * @param String $num2
     * @param Integer $min_sum
     * @param Integer $max_sum
     * @return Integer
     */
    function count($num1, $num2, $min_sum, $max_sum) {
        /**
         * @since 2024-01-16 全局变量
         */
        $this->minSum = $min_sum;
        $this->maxSum = $max_sum;

        /**
         * @since 2024-01-16 防止重复遍历
         */
        $this->dp = [];

        /**
         * @since 2024-01-16 [0, num1]的好整数个数
         * @since 2024-01-16 [0, num2]的好整数个数
         */
        $count1 = $this->GoodCount($num1, 0, 0, true);
        $count2 = $this->GoodCount($num2, 0, 0, true);

        /**
         * @since 2024-01-16 [num1+1, num2]的好整数个数
         * @since 2024-01-16 count都是取模后返回，可能为负数，所以要加pow(10, 9)+7
         */
        $goodCount = ($count2 - $count1 + pow(10, 9)+7) % (pow(10, 9)+7);

        /**
         * @since 2024-01-16 判断num1是不是好整数
         */
        $sum = 0;
        for($i=0; $i<strlen($num1); $i++){
            $sum += intval($num1[$i]);
        }
        if($sum >= $min_sum && $sum <= $max_sum){
            $goodCount++;
        } else {}

        /**
         * @return
         */
        return $goodCount;
    }

    /**
     * @title 返回[0, $num]中好整数个数
     * @author start2004
     * @since 2024-01-16 Created
     *
     * @param String $num
     * @param Int $i
     * @param Int $sum
     * @param Boolean $limit
     * @return Integer
     */
    function GoodCount($num, $i, $sum, $limit){
        /**
         * @since 2024-01-16 数字已经取完，判断是否符合条件
         */
        if($i == strlen($num)){
            if($sum >= $this->minSum && $sum <= $this->maxSum){
                return 1;
            } else {
                return 0;
            }
        } else {}

        /**
         * @since 2024-01-16 是否命中缓存
         */
        $key = "{$num}-{$i}-{$sum}-{$limit}";
        if(isset($this->dp[$key])){
            return $this->dp[$key];
        } else {}

        /**
         * @since 2024-01-16 第i位最大值
         */
        if($limit){
            $max = intval($num[$i]);
        } else {
            $max = 9;
        }

        /**
         * @since 2024-01-16 好整数的个数
         */
        $count = 0;
        for($j=0; $j<=$max; $j++){
            $count += $this->GoodCount($num, $i+1, $sum+$j, $limit && $j==$max);
        }

        /**
         * @return Integer
         */
        return $this->dp[$key] = $count % (pow(10, 9)+7);
    }
}



/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.16_2.php
 */
$datas = [
    ["0", "432", 0, 3], // 20
    // ["1", "12", 1, 8], // 11
    // ["4179205230", "7748704426", 8, 46], // 883045899
    // ["1012640017461217236611", "9234552128261772272769", 67, 148], // 683479047
    // ["0", "4179205230", 0, 7],
    // ["0", "4179205230", 0, 46],
    // ["0", "4179205230", 8, 46],
    // ["0", "7748704426", 0, 7],
    // ["0", "7748704426", 0, 46],
    // ["0", "7748704426", 8, 46],

    ["1", "10000000000000000000000", 1, 200], // 490000
    ["0", "1", 0, 0],
    ["0", "1", 0, 200],
    ["0", "1", 1, 200],
    ["0", "10000000000000000000000", 0, 0],
    ["0", "10000000000000000000000", 0, 200],
    ["0", "10000000000000000000000", 1, 200],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->count(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
